package com.asktgapp.engineer.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.QuestionDetailVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.utils.ImageDisplayUtil;

import java.util.Date;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/3/2
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class AnswerQuestionFragment extends BaseFragment {
    //头部
    @InjectView(R.id.iv_head_ask)
    ImageView mQuestionHead;
    @InjectView(R.id.tv_ask_name)
    TextView mAskName;
    @InjectView(R.id.tv_ask_time)
    TextView mAskTime;
    @InjectView(R.id.tv_question)
    TextView mQuestion;
    @InjectView(R.id.et_answer)
    EditText mAnser;
    @InjectView(R.id.tv_commit)
    TextView mCommit;
    private int mQuestionId;
    private QuestionDetailVO mQuestionDetailVO;


    private void setUI(QuestionDetailVO bean) {
        ImageDisplayUtil.loadCircleHeadImage(getActivity(), mQuestionHead, bean.getQUserPic());
        mAskName.setText(bean.getQUersName());
        mAskTime.setText(Utils.getDateYMD(bean.getAddTime()));
        mQuestion.setText(bean.getQContent());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_answer_question, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mQuestionId = getActivity().getIntent().getIntExtra("id", 0);
        mCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upAnswer();
            }
        });
    }

    private void upAnswer() {
        String answer = mAnser.getText().toString().trim();
        if (Util.isEmpty(answer)) {
            showTost("请输入您的答案", 1);
            return;
        }
        if (answer.length() > 500) {
            showTost("请将答案控制在500字内，当前字数：" + answer.length(), 1);
            return;
        }
        if (!Utils.noContainsEmoji(answer)) {
            showTost("不支持输入emoji表情", 1);
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("id", mQuestionId);
        params.put("content", answer);
        showProgress(getClass().getSimpleName());
        if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER))
            params.put("isProjector", "1");
        else
            params.put("isProjector", "0");

        Call<ApiResponseBody> call = apiservice.projecterSubProblem(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("提交成功", 0);
                    getActivity().finish();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }

    @Override
    public void loadData() {
        visibleLoading();
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("id", mQuestionId);
        Call<ApiResponseBody<QuestionDetailVO>> call = apiservice.getQuestionInfo(params);
        call.enqueue(new Callback<ApiResponseBody<QuestionDetailVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<QuestionDetailVO>> call, Response<ApiResponseBody<QuestionDetailVO>> response) {
                inVisibleLoading();
                if (response.isSuccessful()) {
                    mQuestionDetailVO = response.body().getResult();
                    setUI(mQuestionDetailVO);
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<QuestionDetailVO>> call, Throwable t) {
                inVisibleLoading();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else
                    showTost(apiException.getMessage(), 1);
            }
        });
    }
}
